package com.xxx.meter.model.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.xxx.meter.ConfigClass;


/**
 * Glide工具类
 */
public class GlideUtil {

    public static void loadImage(Context context, String url, final ImageView imageView) {
        if (context != null) {
            Glide.with(context).load(url)
                    .error(ConfigClass.ERROR_IMAGE)
                    .into(imageView);
        }
    }

    public static void loadCircle(final Context context, String url, final ImageView imageView) {
        if (context != null) {
            Glide.with(context).load(url).asBitmap()
                    .error(ConfigClass.ERROR_IMAGE)
                    .into(new BitmapImageViewTarget(imageView) {
                        @Override
                        protected void setResource(Bitmap resource) {
                            RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                            circularBitmapDrawable.setCircular(true);
                            imageView.setImageDrawable(circularBitmapDrawable);
                        }
                    });
        }
    }

    public static void loadFillet(final Context context, String url, final ImageView imageView) {
        if (context != null) {
            Glide.with(context).load(url).asBitmap()
                    .error(ConfigClass.ERROR_IMAGE)
                    .into(new BitmapImageViewTarget(imageView) {
                        @Override
                        protected void setResource(Bitmap resource) {
                            super.setResource(resource);
                            RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                            circularBitmapDrawable.setCornerRadius(10);
                            imageView.setImageDrawable(circularBitmapDrawable);
                        }
                    });
        }
    }
}
